*** Set paths
local dir "..."
local dropbox_data "..."
local dropbox_data_create "..."
local local_data "..."
local dropbox_fig "..."
local dropbox_tab "..."


************************************************************
**** CREATE PRE-AUCTION ANNOUNCEMENT INDICATOR
************************************************************
 
use "`local_data'/data_base_bonds_bills.dta", clear 
keep if market_type=="auction"| market_type=="secondary"

if $allauctions ==0{ // regular auctions only

	keep if (market_type=="auction" & (auctiontype=="TBREG" | auctiontype=="BDREG")) | market_type=="secondary"
	gen last_auction=AuctionDate  if auctiontype=="TBREG" | auctiontype=="BDREG" 
		
	g  double auc_announce = dhms(date, 10, 35, 0) 	if  auctiontype=="TBREG" 
	replace auc_announce   = dhms(date, 12, 05, 0) if  auctiontype=="BDREG" 
	format  auc_announce %tc
	
	*latest annoucement per auction date 
	bys date: egen a0 = max(auc_announce)
	format a0 %tc
	replace auc_announce=a0 
}

if $allauctions ==2{ // all but switch auctions
	keep if (market_type=="auction" & ///
			(auctiontype=="TBREG" | auctiontype=="BDREG" | auctiontype=="CM" | auctiontype=="NF") ) | market_type=="secondary"		
	gen last_auction=AuctionDate  if auctiontype=="TBREG" | auctiontype=="BDREG" | auctiontype=="CM" | auctiontype=="NF"
	
	g double auc_announce = dhms(date, 10, 35, 0) if  auctiontype=="TBREG" | auctiontype=="CM" | auctiontype=="NF" 
	replace auc_announce =  dhms(date, 12, 05, 0) if  auctiontype=="BDREG" 
	format  auc_announce %tc
	
	*latest annoucement per auction date 
	bys date: egen a0 = max(auc_announce)
	format a0 %tc
	replace auc_announce=a0 
}

*Create days post auction
format last_auction %td
sort  date  market_type
replace last_auction = last_auction[_n-1] if last_auction >= . 
gen days_post_auction = date - last_auction

*Collapse per date and keep the days-post auction and the announcement time for an auction day
collapse IIROC_QUANTITY, by(date days_post_auction auc_announce)
bys date: gen a0=_N // Careful: this only works if there are no TBREG and BDREG auctions at the same date, which is the case in the data
tab a0 
keep date days_post_auction auc_announce
tempfile tempdates
save  `tempdates' , replace


************************************************************
**** ADDITIONAL SAMPLE RESTRICTION FOR MODEL ESTIMATION
************************************************************

*load restricted sample
use "`local_data'/data_base_bills_clients_matlab_2022b.dta", clear


if($parentdealer ==1){
	replace bidderid=46 if bidderid==46001
	replace bidderid=24 if bidderid==24001
	replace bidderid=18 if bidderid==18001
}

*drop in-house 
drop if gleif_related=="Y"

*drop dates before July 29 2019
keep if date <=td(29jul2019) 

*keep only primary dealers (they are also on CanDeal)
keep if bidderid==18 | bidderid==24 | bidderid==46 | bidderid==61 | bidderid==67|bidderid==77 | bidderid==83 | bidderid==88 | bidderid==93 | bidderid==94 | bidderid==18001 | bidderid==24001 | bidderid==46001

*drop 1 dealer
drop if bidderid==83  // need to drop because of misreporting: 83 stops reporting that it trades on CanDeal in May 2016 

*drop 2 holidays,els e these create issues with the matlab code, but given the few data points won't matter for the restults. 
drop if date==td(01jul2016) | date==td(05aug2019)

*drop large trade sizes (for robustness analyses)
if $quantityrobust ==1 {

	drop lei
	egen lei= group(LEI)
	egen lei_max = max(lei)
	bys LEI: gen ids = _n if LEI=="anonymous"	
	replace lei = lei_max+ids  if LEI=="anonymous"	
	
	bys lei: egen pQ99 = pctile(IIROC_QUANTITY), p(95)

	drop if IIROC_QUANTITY>pQ99 &  LEI!="anonymous"	 // drop trades in 5% highest percentile of all trades of an investor
	drop lei
}

*drop pre-auction outcome announcement 
merge m:1 date using `tempdates'
keep if _merge==3
drop _merge
drop if days_post_auction==0 & exec_time<auc_announce

*drop days before the first auction
unique date if days_post_auction <=$exclude_dates
keep if days_post_auction <=$exclude_dates // with exclude_date=10000, this only excludes few trading dates before the first auction. 

	*Check averge number of days post an auction
	tabstat days_post_auction
	
*Create home dealer for the subsample we must use to use the instrument 
do "Homedealer.do"

	
***********************************************************************************************
*** USE THE NORMALIZED YIELDS
***********************************************************************************************

if $norm ==1{	
	gen IIROC_YIELD_org = IIROC_YIELD
	gen midyield_bl_org = midyield_bl
	gen askyield_bl_org = askyield_bl
	gen bidyield_bl_org = bidyield_bl
	gen askyield_can_org = askyield_can_norm
	gen bidyield_can_org = bidyield_can_norm

	replace IIROC_YIELD  = yield 
	replace midyield_bl  = midyield_bl_norm
	replace askyield_can = askyield_can_norm
	replace bidyield_can = bidyield_can_norm
}


**********************************************************************************************
* CHOOSE PERIOD
***********************************************************************************************

*** Determine the period 
if($week ==1){
	egen period=group(year week)
}

if($week ==2){
	egen period=group(year month)
}

if($week ==0){
	egen period=group(date)
}
if($week ==-1){

	preserve 
	collapse IIROC_QUANTITY, by(date week year days_post_auction auc_announce)
	egen yw=group(week year)
	unique yw
	unique yw if auc_announce!=. // 30 weeks in sample without an auction. 155 with an auction 
	gen aucdate = date if auc_announce!=.
	format aucdate %td 
	egen period=group(aucdate)
	sort date 
	replace period=period[_n-1] if period==.
	bys period: gen a0=_N 
	tab a0
	tabstat a0
	
	
	*Alternative 1)
	*keep if a0>=2
	*tabstat a0
	
	*Alternative 2)
	*gen month=month(date)
	*gen day = day(date)
	*gen dow = dow( mdy( month, day, year) )
	*bys period: egen mondayauc=max(dow)
	
	*keep if mondayauc==1 /// doesn't exist :()

	keep date period
	tempfile tempperiod
	save  `tempperiod' , replace
	restore 
	
	merge m:1 date using `tempperiod'
	drop _merge 
}

drop if period==. 

*Create a large trade indicator 
if($largetrade ==1){
	gen large_trade = (IIROC_QUANTITY>25)
}
if($largetrade ==0){
	gen large_trade = 0
}
	

**********************************************************************************************
* WHICH INVESTORS TO USE 
***********************************************************************************************

*when using all investors ot improve power
if ($keepall )==0{	
	
	*Separate the retail investors
	preserve
	keep if retailer==1
	save "`local_data'/data_base_retailer_2022.dta", replace
	*save "`local_data'/data_base_retailer.dta", replace
	restore 
	
	*Only keep LEIs		
	keep if trackclient=="market-wide"
}
	
	
**********************************************************************************************
* MARKET SHARES
***********************************************************************************************

do "Marketshares.do"
tempfile temp
save  `temp' , replace


***********************************************************************************************
*** MERGE WITH AUCTION DATA AND SAVE FOR MATLAB CODE
***********************************************************************************************

use "`local_data'/data_auction_expecations.dta", clear  // this data is crated in instrument.do
drop if bidderid==83

if($parentdealer ==1){
	replace bidderid=46 if bidderid==46001
	replace bidderid=24 if bidderid==24001
	replace bidderid=18 if bidderid==18001
	
	foreach i of numlist 46 24 18{
		foreach var of varlist exp exp_q  exp_pc exp_q_pc   issuesum   tot_dem_isin tot_won_isin {
			bys bidderid date isin: egen a0 = mean(`var') if bidderid==`i'
			replace `var'=a0 if `var'==. & bidderid==`i'
			drop a0 
		}
		foreach var of varlist  exp_tot exp_q_tot  exp_pc_tot exp_q_pc_tot  issuesum_tot tot_dem tot_won {
			bys bidderid date : egen a0 = mean(`var') if bidderid==`i'
			replace `var'=a0 if `var'==. & bidderid==`i'
			drop a0
		}
	}
}

append using  `temp'


*fill in auction data for the rest of the day 
foreach var of varlist exp exp_q  exp_pc exp_q_pc   issuesum   tot_dem_isin tot_won_isin {
bys bidderid date isin: egen a0 = max(`var')
replace `var'=a0 if `var'==.
drop a0 
}

foreach var of varlist  exp_tot exp_q_tot  exp_pc_tot exp_q_pc_tot  issuesum_tot tot_dem tot_won {
bys bidderid date : egen a0 = max(`var')
replace `var'=a0 if `var'==.
drop a0 
}


*copy expectations downward
bys bidderid isin (date): replace exp_q = exp_q[_n-1] if exp_q>=.
bys bidderid  (date): replace exp_q_tot = exp_q_tot[_n-1] if exp_q_tot>=.
bys bidderid isin (date): replace exp = exp[_n-1] if exp>=.
bys bidderid  (date): replace exp_tot = exp_tot[_n-1] if exp_tot>=.

bys bidderid isin (date): replace exp_q_pc = exp_q_pc[_n-1] if exp_q_pc>=.
bys bidderid  (date): replace exp_q_pc_tot = exp_q_pc_tot[_n-1] if exp_q_pc_tot>=.
bys bidderid isin (date): replace exp_pc = exp_pc[_n-1] if exp_pc>=.
bys bidderid  (date): replace exp_pc_tot = exp_pc_tot[_n-1] if exp_pc_tot>=.

*copy down the issued amount		
bys bidderid isin (date): replace issuesum = issuesum[_n-1] if issuesum>=.
bys bidderid  (date): replace issuesum_tot = issuesum_tot[_n-1] if issuesum_tot>=.

*per auction day (ignore the timing of the bid)
bys bidderid date: egen a0=max(tot_won)
bys bidderid date: egen a1=max(tot_dem)
bys bidderi isin date: egen a2 = max(tot_won_isin)
replace tot_won=a0
replace tot_dem=a1
replace tot_won_isin = a2

bys bidderid (date): replace tot_won = tot_won[_n-1] if tot_won>=.
bys bidderid (date): replace tot_dem = tot_dem[_n-1] if tot_dem>=.
bys bidderid isin (date): replace tot_won_isin = tot_won_isin[_n-1] if tot_won_isin>=.

gen share_won_of_dem 	   = tot_won/tot_dem*100
gen won_of_dem_tot_sim     = tot_won - exp_q_tot // aggregated per isin
gen won_of_dem_pc_tot_sim  = tot_won - exp_q_pc_tot // aggregated per isin
gen won_of_dem_sim         = tot_won_isin - exp_q   // per isin
gen share_of_dem_tot_sim   = tot_won/issuesum_tot*100 - exp_tot // aggregated per isin
gen share_of_dem_sim       = tot_won_isin/issuesum*100 - exp  // per isin
drop a0 a1 
		
*Pick instrument	
if $instrument ==0{
replace share_won_of_dem = tot_won
}

if $instrument ==4{
replace share_won_of_dem= won_of_dem_tot_sim // amount won - expected amount won| bid, both per date (aggregating over isin) 
}

*Throw out autions
keep if market_type=="secondary"

*Quotes weekly
bys period bidderid IIROC_SIDE trading_venue large_trade: egen a0 = mean(IIROC_YIELD)  if trading_venue=="CanDeal" 
bys period bidderid IIROC_SIDE large_trade: egen quote = max(a0)
drop a0 

*Save full data for matlab input
tempfile temprest
save  `temprest' , replace


***********************************************************************************************
*** SUPPORTING EVIDENCE REGRESSION: P=WTP
***********************************************************************************************

if($week ==0 & $homedealer ==0 & $largetrade ==0){
	
	************************************************
	**** APPENDIX TABLE A3
	************************************************
	drop markup2
	gen markup2  	=  yield - midyield_bl_norm 
	replace markup2 = -markup2 if IIROC_SIDE=="SELL"
	replace share_won_of_dem=share_won_of_dem/1000 // in billion
	eststo: reghdfe yield midyield_bl_norm share_won_of_dem if trading_venue=="OTC" & IIROC_SIDE=="BUY"  & trackclient!="no", absorb(date bidderid lei_id) 
	eststo:  reghdfe yield midyield_bl_norm share_won_of_dem if trading_venue=="OTC" & IIROC_SIDE=="SELL" & trackclient!="no" , absorb(date bidderid lei_id) 
	eststo: reghdfe quote midyield_bl_norm share_won_of_dem  if trading_venue=="CanDeal" & IIROC_SIDE=="BUY" & trackclient!="no", absorb(date bidderid lei_id) 
	eststo:  reghdfe quote midyield_bl_norm share_won_of_dem  if trading_venue=="CanDeal" & IIROC_SIDE=="SELL" & trackclient!="no" , absorb(date bidderid lei_id) 
	esttab using  "`dropbox_tab'/reg_wtp_both.tex",  se ar2 nobaselevels  label mtitle("(1)" "(2)" "(3)" "(4)") ///
	title("How bilateral yields and platform quotes change given supply shocks")  nonumbers  replace 
	eststo clear
	
}

***********************************************************************************************
*** PREPARE VARIABLES FOR MARKET-SHARE REGRESSION
***********************************************************************************************

*Keep Buy-side trades on CanDeal
keep if trading_venue=="CanDeal"  & IIROC_SIDE=="BUY"  

*Average instruments per week 
bys period bidderid : egen a0 = mean(share_won_of_dem) 
replace share_won_of_dem=a0
drop a0 

*Normalize the quotes by q0
bys period bidderid large_trade: egen a0 = mean(IIROC_YIELD) if bidderid==$basedealer
bys period large_trade: egen quote_0 = max(a0)
gen quote_rel = quote - quote_0
drop a0 

*To normalize the instrument by instrument0
bys period bidderid : egen a0 = mean(share_won_of_dem) if bidderid==$basedealer 
bys period: egen share_won_of_dem_0 = max(a0)
drop a0

*Regression preparastion
destring bidderid, replace
label var IIROC_QUANTITY "quantity in mil CAD"
label var quote "quote"
label var share_won_of_dem "quote"

*Market share in Benchmark model
gen a0 = share_Ei if trading_venue=="CanDeal" & IIROC_SIDE=="BUY" & bidderid==$basedealer
bys period IIROC_SIDE large_trade: egen share_E0 = max(a0) 
replace share_E0 = 0 if share_E0==. & market_type=="secondary"
drop a0
*log ratio
gen double share_ratio = share_Ei/share_E0
gen double log_shares = log(share_ratio)

*Market shares in Homedealer model
gen a0 = share_Ei_H if trading_venue=="CanDeal" & IIROC_SIDE=="BUY" & bidderid==$basedealer
bys  homedealerID period IIROC_SIDE large_trade: egen share_E0_H = max(a0)  
replace share_E0_H = 0 if share_E0_H==. & market_type=="secondary" 
drop a0
*log ratio
gen double share_ratio_H = share_Ei_H/share_E0_H
gen double log_shares_H = log(share_ratio_H)

